<?php
namespace Pinenut;

use Libmail\Mail as Adapter;
class Mail {
	protected $config;
	protected $adapter;

	public $from, $to, $subject, $content;
	
	function __construct($params = array()) {
		foreach ($params as $key => $value) {
			if (property_exists($this, $key)) {
				$this->$key = $value;
			}
		}

		$this->config = \SmtpConfig::get();
	}
	
	function send() {
		$this->adapter = new Adapter();
		$mailType = strpos(mb_strtolower($this->content, 'UTF-8'), '<html') !== false ? 'html' : '';

		// пока тупо использую либу влоб
		$this->adapter->From($this->from?$this->from:$this->config['from']);
		$this->adapter->To($this->to);
		$this->adapter->Subject($this->subject);
		$this->adapter->Body($this->content, $mailType);
		$this->adapter->smtp_on($this->config['host'], $this->config['login'], $this->config['password']);
		if (!$this->adapter->Send()) {
			throw new \Exception($this->adapter->status_mail['message']);
		}
	}
}